package com.fire.user.util.auth;

import com.alibaba.druid.support.json.JSONUtils;
import com.fire.user.util.DateUtil;

import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author raoyuxing
 * @date 2018/8/9 11:16
 */
public class Sign {
    public static String guangdongyidong() {
        Map<String, Object> map = new TreeMap<String, Object>();
        map.put("version", "1.0");
        map.put("id", "021004");
        map.put("idtype", "0");
        map.put("msgid", "1534313855807");
        map.put("token", "SSO_ATe8d9f0e89b2cd2a6a458a0b7a2");
        map.put("systemtime", "20180815141735807");
        map.put("key", "123456");
        map.put("apptype", "5");
        String enStr = mapToString(map);
        String sign = md5(enStr);
        System.out.println("sign=" + sign);
        return sign;
    }

    public static String SsoCoupletTest(){
        Map request = new HashMap();
        Map body = new HashMap();
        Map header = new HashMap();

        body.put("token","8484010001320200344E6A5A4551554D784F444E474E446C434E446779517A673340687474703A2F2F3139322E3136382E31322E3233363A393039302F0300040353EA68040006313030303030FF00203A020A143C6703D7D0530953C760744C7D61F5F7B546F12BC17D65254878748C");
        header.put("version","1.0");
        header.put("msgid",System.currentTimeMillis());
        header.put("systemtime", DateUtil.formatDate(new Date(),"yyyyMMddHHmmssSSS"));
        header.put("id","021004");
        header.put("idtype","0");
        header.put("apptype","5");
        Map<String, Object> enMap = new TreeMap<String, Object>();
        enMap.put("token",body.get("token"));
        enMap.put("version",header.get("version"));
        enMap.put("msgid",header.get("msgid"));
        enMap.put("systemtime",header.get("systemtime"));
        enMap.put("id",header.get("id"));
        enMap.put("key", "123456");
        enMap.put("idtype",header.get("idtype"));
        enMap.put("apptype",header.get("apptype"));
        String enStr = Sign.mapToString(enMap);
        header.put("sign",Sign.md5(enStr));

        request.put("body",body);
        request.put("header",header);

        String json = JSONUtils.toJSONString(request);
        System.out.println(json);
        return json;
    }

    /**
     *
     * 将Map转换为String
     *
     * @Title: map2String
     * @param map
     * @return
     * @author: yanhuajian 2013-7-21下午7:25:08
     */
    public static String mapToString(Map<String, Object> map) {
        if (null == map || map.isEmpty()) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            sb.append(entry.getValue());
        }

        return sb.toString();
    }

    public static String md5(String text) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(text.getBytes("UTF-8"));
            byte[] b = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


}
